#include "TcpClient.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Utils.h"


#include<android/log.h>
#define TAG "TcpClient" // 这个是自定义的LOG的标识
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // 定义LOGD类型

TcpClient::TcpClient()
{
	m_pOnRecv = NULL;
//	m_Socket = -1;
    fds = new struct pollfd;
    memset(fds, 0, sizeof(struct pollfd));
	m_nPort  = 0;
	m_hThread = 0;
	m_bRunning = 0;
	m_bConnected = 0;
}

TcpClient::~TcpClient()
{
	Close();
	delete fds;
}

int TcpClient::Open(int nPort)
{
	m_nPort = nPort;
	
	Close();
	Connect();
	
	return BeginThread();
}

void TcpClient::Close()
{
	m_bRunning = 0;
	m_bConnected = 0;

//	if (-1 != m_Socket) {
//		close(m_Socket);
//		m_Socket = -1;
//	}
    if (fds->fd) {
        close(fds->fd);
    }
}

void TcpClient::SetOnRecv(IOnRecv* pOnRecv)
{
	m_pOnRecv = pOnRecv;
}

int TcpClient::Connect()
{
//	if (-1 != m_Socket) {
//		return 1;
//	}
	if (fds->fd) {
		return 1;
	}

//	m_Socket = socket(AF_INET, SOCK_STREAM, 0);
	fds->fd = socket(AF_INET, SOCK_STREAM, 0);

//    if (-1 == m_Socket) {
//        return 0;
//    }
	if (-1 == fds->fd) {
		return 0;
	}
	fds->events = POLLIN;

	int flags = fcntl(fds->fd, F_GETFL, 0);
    fcntl(fds->fd, F_SETFL, flags|O_NONBLOCK);

    struct sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(m_nPort);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    int result = -1;
    int nRet = connect(fds->fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr));
    if (0 == nRet) {
        result = 0;
    } else if (EINPROGRESS == errno) {
        result = 0;
	}

	if (result < 0) {
		close(fds->fd);
		return 0;
	}

	m_bConnected = 1;
	return 1;
}

int TcpClient::BeginThread()
{
	if (0 != m_hThread) {
		m_hThread = 0;
	}

	m_bRunning = 1;

	if (pthread_create(&m_hThread, NULL, ThreadProc, this) != 0)
    {
        m_bRunning = 0;
    }

	return m_bRunning;
}

void* TcpClient::ThreadProc(void* lpParam)
{
	return ((TcpClient *)lpParam)->ThreadFunc();
}

int TcpClient::Send(const byte* pData, int nLength)
{
	if (-1 == fds->fd) {
		return 0;
	}

	int iResult = 0;
	iResult = send(fds->fd, (const char*)pData, nLength, 0);
	if (-1 == iResult) {
		Close();
		return 0;
	}

	return 1;
}

int TcpClient::IsRunning()
{
	return m_bRunning;
}

int TcpClient::IsConnected()
{
	return m_bConnected;
}

void* TcpClient::ThreadFunc()
{
	while (m_bRunning)
	{
		Work();

		m_bConnected = 0;
		if (!m_bRunning) {
			break;
		}

		usleep(50000);

//		if (-1 != m_Socket) {
//			close(m_Socket);
//			m_Socket = -1;
//		}
        if (! fds->fd) {
            Close();
        }
	}

	return NULL;
}

void TcpClient::Work()
{
	if (!Connect()) {
		return;
	}

	while (m_bRunning)
	{
		int ret = 0;
		int readLen = 0;

		ret = poll(fds, 1, 5000);
		if(-1 != ret)
		{
			readLen = recv(fds->fd, (char*)m_RecvBuf, 3000, 0);
			if(readLen > 0) {
				m_pOnRecv->OnRecv(m_RecvBuf, readLen);
			}
			else{
			}
		}
	}
}
